<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/1/19
 * Time: 16:19
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\api\controller;

use app\api\model\mysql\MallUserAddress;
use app\common\lib\Show;
use app\common\lib\Status;
use think\App;
use think\facade\Log;
use think\facade\Validate;

class Address extends Base
{
    protected $modelObj = null;

    public function __construct(App $app)
    {
        $this->modelObj = new MallUserAddress();
        parent::__construct($app);
    }

    /**
     * 获取所有
     * @return \think\response\Json
     */
    public function getAddressByInfo()
    {
        $openid = $this->request->param('openid', '', 'trim');
        try {
            $address = $this->modelObj->getUserAddressByList($openid);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, "查询地址错误");
        }
        return Show::json_show(Status::mini_normal, "ok", $address);
    }

    /**
     * 新增
     * @return \think\response\Json
     */
    public function addAddress()
    {
        $openid = $this->request->param('openid', '', 'trim');
        $realname = $this->request->param('realname', '', 'trim');
        $mobile = $this->request->param('mobile', '', 'trim');
        $locatarea = $this->request->param('locatarea', '', 'trim');
        $address = $this->request->param('address', '', 'trim');
        $cityarea = $this->request->param('cityarea', '', 'trim');
        $areacode = $this->request->param('areacode', '', 'trim');
        $lat = $this->request->param('lat', '', 'trim');
        $lng = $this->request->param('lng', '', 'trim');
        $is_default = $this->request->param('isdefault', 0, 'intval');
        $id = $this->request->param('id',0,'intval');

        $validate = Validate::rule([
            'realname' => "require",
            "mobile" => "require|mobile",
            "cityarea" => "require",
            "address" => "require",
            "lat" => "require",
            "locatarea" => "require"
        ])->message([
            "realname.require" => "请输入姓名",
            "mobile.require" => "请输入手机号",
            "mobile.mobile" => "手机号不正确",
            "cityarea.require" => "请选择省市区",
            "address.require" => "请选择地址",
            "lat.require" => "请输入详情地址",
            "locatarea.require" => "请选择地址"
        ]);
        $data = [
            "realname" => $realname,
            "mobile" => $mobile,
            "address" => $address,
            "lat" => $lat,
            "lng" => $lng,
            "cityarea" =>$cityarea,
            "areacode" =>$areacode,
            "isdefault" => $is_default,
            "openid" => $openid,
            "locatarea" => $locatarea,
        ];

        if (!$validate->check($data)) {

            return Show::json_show(Status::mini_error, $validate->getError());
        }

        //先更新其他
        $this->modelObj->startTrans();
        try {
            $this->modelObj->where('openid', $openid)
                ->update([
                    "isdefault" => 0,
                    "update_time" => time()
                ]);
            if($id>0){
                $this->modelObj->where("id",$id)->save($data);
            }else{
                $this->modelObj->save($data);
            }
            $this->modelObj->commit();
        } catch (\Exception $e) {
            $this->modelObj->rollback();
            Log::error("user_update:" . $e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            return Show::json_show(Status::mini_error, "操作失败");
        }
        return Show::json_show(Status::mini_normal, "ok");
    }
}